package messagePacket;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import Util.Util;
import entity.ChatMessage;
import io.netty.channel.Channel;
import server.ChattingServeHandler;

/**
 * @author gg_boy
 * @version $Rev$
 * @des ${TODO}
 * @updateAuther $Auther$
 * @updateDes ${TODO}
 */
public class SearchFriendPacket extends Packet{
    private String sendUser;

    public SearchFriendPacket() {
		
	}
    public SearchFriendPacket(String sendUser) {
        this.sendUser = sendUser;
    }

    @Override
    public int getPacketType() {
        return super.getPacketType();
    }

    @Override
    public void setPacketType(int packetType) {
        super.setPacketType(packetType);
    }

    @Override
    public void decode(byte[] buffer) throws UnsupportedEncodingException {
        int start_pos = 4;
        // 获取长度
        int length = Util.bytes2int(buffer, start_pos);
        start_pos += 4;

        this.sendUser = new String(buffer, start_pos, length, "UTF-8");
        start_pos += length;

    }

    @Override
    public byte[] encode() throws UnsupportedEncodingException {
        int sendUserLength = this.sendUser.getBytes("UTF-8").length;
        int totalLength = 4 + 4 + sendUserLength;
        byte[] buffer = new byte[totalLength];
        int offset = 0;

        // add packet type to buffer
        System.arraycopy(Util.int2bytes(Util.PACKET_SEARCH_FRIEND), 0, buffer, offset, 4);
        offset += 4;

        // the four bytes store send user name length
        System.arraycopy(Util.int2bytes(sendUserLength), 0, buffer, offset, 4);
        offset += 4;

        // copy send user to buffer
        System.arraycopy(sendUser.getBytes("UTF-8"), 0, buffer, offset, sendUserLength);
        offset += sendUserLength;

        return buffer;
    }

 
    public String getSendUser() {
		return sendUser;
	}

	public void setSendUser(String sendUser) {
		this.sendUser = sendUser;
	}

	@Override
	public void process(Channel channel, Packet packet) {
    	SearchFriendPacket searchFriendPacket = (SearchFriendPacket) packet;
    	showFirends(channel, searchFriendPacket);
	}
    
	private void showFirends(Channel newchannel, SearchFriendPacket cmsg) {
		String friendList = "";
		for (HashMap.Entry<String, HashMap<Integer, String>> friend : ChattingServeHandler.friends.entrySet()) {
			System.out.println(friend.getKey()+"    "+ friend.getValue());
			if(friend.getKey().equals(cmsg.getSendUser())) {
				for (HashMap.Entry<Integer, String> f : friend.getValue().entrySet()) {
					if(!f.getValue().equals(cmsg.getSendUser())) {						
						friendList += "\t" + f.getValue();
					}
				}
			}
		}
		MessagePacket message = new MessagePacket("服务器", cmsg.getSendUser(), "你的好友有"+friendList, 2);
		newchannel.writeAndFlush(message);
	}
	
	@Override
    public String toString() {
        return "SearchFriendPacket{" +
                "sendUser='" + sendUser + '\'' +
                '}';
    }
	
}
